package com.rr.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @author zhuangcanrun
 * @Date 2022/05/26/21:29
 * @description
 **/

@Configuration
public class ThreadConfig {
    /*
    * 阻塞队列：
    * ArrayBlockingQueue：用数组实现的有界阻塞队列，先进先出
    * LinkedBlockingQueue：链表结构的有界阻塞队列 ，先进先出，默认长度Integer.MAX_VALUE，可能会出现容量危险
    * PriorityBlockingQueue：支持线程优先级排序的无界队列
    * DelayQueue：可以指定多久才能从队列中获取元素
    * SynchronousQueue：
    * LinkedTransferQueue
    * LinkedBlockingDeque
    * */
    /*
    * 拒绝策略
    * ThreadPoolExecutor.AbortPolicy（默认）：丢弃任务并抛出异常
    * ThreadPoolExecutor.DiscardPolicy：丢弃任务但不抛出异常
    * ThreadPoolExecutor.DiscardOldestPolicy：丢弃队列最前面的任务，再重新提交被拒绝的任务
    * ThreadPoolExecutor.CallerRunsPolicy：由调用线程（提交任务的线程）处理该任务
    * */
    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        return new ThreadPoolExecutor(1,
                1,
                5L,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue<>(1),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }
}
